<template>
	<view class="content">
		<div class="banner"></div>
		<view class="content_container">
			<view class="title_tip_info">请输入短信验证码</view>
			<view class="title_sub_tip_info">验证码已发送至{{newMobileFormatVal}}</view>
			<view class="sms_code_input_container">
				<xtVerifyCode :isFocus="verifyCodeFocus" v-model="value" type="bottom" inputType="text" :size="4"
					@confirm="finishInput" @inputFocus="inputFocus"></xtVerifyCode>
			</view>
			<view class="errmsg_box">{{errMsg}}</view>
			<view class="smscode_get_info">
				<view v-if="smsSeconds===0">
					<text @click.stop="getSmsCode">重新获取验证码</text>
				</view>
				<view v-else>
					<text>{{smsSeconds}}s </text>
					后重新发送
					<view>没收到验证码？</view>
				</view>
			</view>
			<view class="btn_box"><button :disabled="value.length<4" @click="verifyCode">下一步</button></view>
		</view>
		<view class="my_interest">
			<image src="../../static/icon/myInterest.png"></image>
			<text>查看我的权益</text>
		</view>
		<number-keyboard ref='KeyboarHid' v-model="value" :psdLength='valArr[currentKey].lng'></number-keyboard>
	</view>
</template>

<script>
	import numberKeyboard from '../components/number-keyboard/number-keyboard.vue'
	import xtVerifyCode from '../components/xt-verify-code/xt-verify-code.vue'
	export default {
		components: {
			numberKeyboard,
			xtVerifyCode
		},
		data() {
			return {
				errMsg: '', //错误信息
				mobile: '',
				value: '', //数字键盘输入的值
				currentKey: 1,
				valArr: [{
					val: '',
					lng: 11
				}, {
					val: '',
					lng: 4
				}],

				newMobileFormatVal: '',

				verifyCodeFocus: false,

				smsSeconds: 60,

				timer: null
			}
		},
		onLoad() {
			let token = uni.getStorageSync("token");
			if(token!=null && token!=''){
				uni.navigateTo({
					url:'/pages/myInterest/index'
				})
			}else{
				let query = this.$route.query;
				this.mobile = query.mobile;
				if (this.mobile && this.mobile != null && this.mobile != '') {
					this.newMobileFormatVal = " +86 " + this.mobile.slice(0, 3) + ' ' + this.mobile.slice(3, 7) + ' ' + this
						.mobile
						.slice(7, 11);
					this.getSmsCode();
					this.$nextTick(() => {
						this.errMsg = '';
						this.verifyCodeFocus = false;
						this.$refs.KeyboarHid.open();
					})
				}else{
					uni.navigateTo({
						url:'/pages/index/index'
					})
				}
			}
		},
		watch: {
			value: function(newVal, oldVal) {

			}
		},
		methods: {
			inputFocus() {
				this.$refs.KeyboarHid.open();
			},
			startTimerInterval() {
				this.timer = setInterval(() => {
					this.smsSeconds--;
					// 当倒计时为0时，启用按钮并清除计时器
					if (this.smsSeconds === 0) {
						if (this.timer != null) {
							clearInterval(this.timer);
						}
					}
				}, 1000);
			},
			/**
			 * 校验验证码
			 * @param {Object} e
			 */
			verifyCode(e) {
				//重置错误信息
				this.errMsg = '';
				this.verifyCodeFocus = false;
				this.$refs.KeyboarHid.close();
				if (this.value.length != 4) {
					this.errMsg = "请输入4位验证码";
					setTimeout(() => {
						this.verifyCodeFocus = true;
						this.$refs.KeyboarHid.open();
					}, 500)
					return;
				}
				uni.request({
					url: "/api/h5/validateSmsCode",
					method: 'POST',
					data: {
						phoneNumber: this.mobile,
						code: this.value
					},
					success: (res) => {
						if (res.data.code == 200) {
							let token = res.data.data;
							uni.setStorageSync('token', token);
							uni.navigateTo({
								url: '/pages/myInterest/index'
							})
						} else if (res.data.code == 901) {
							this.errMsg = res.data.msg;
							clearInterval(this.timer);
							this.smsSeconds = 0
						} else {
							this.errMsg = res.data.msg;
							this.verifyCodeFocus = true;
							this.$refs.KeyboarHid.open();
						}
					},
					fail: (err) => {
						this.errMsg = '短信验证码验证接口异常';
					}
				})
			},
			getSmsCode() {
				uni.request({
					url: "/api/h5/sendSms",
					method: 'POST',
					data: {
						phoneNumber: this.mobile
					},
					success: (res) => {
						if (res.data.code == 200) {
							this.startTimerInterval()
						} else {
							this.errMsg = res.data.msg;
						}
					},
					fail: (err) => {
						this.errMsg = "短信发送接口异常";
					}
				})

			}
		}
	}
</script>
<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
	}

	//banner
	.banner {
		width: 100%;
		height: 294rpx;
		background-image: url("../../static/banner.jpg");
		background-size: 100% 100%;
	}

	//主内容区容器
	.content_container {
		padding: 40rpx 64rpx 0 64rpx;
	}

	//标题样式
	.title_tip_info {
		font-size: 40rpx;
		font-weight: 600
	}

	//错误信息样式
	.errmsg_box {
		margin-top: 24rpx;
		font-size: 24rpx;
		color: #FF7541
	}

	//副标题样式
	.title_sub_tip_info {
		margin-top: 8rpx;
		font-size: 24rpx;
		color: #3D3D3D
	}

	//按钮
	.btn_box {
		margin-top: 40rpx;

		button {
			font-size: 32rpx;
			color: #ffffff;
			background-image: linear-gradient(270deg, #FF5745 0%, #FF8042 100%);
			height: 96rpx;
			line-height: 96rpx;
			border-radius: 48rpx;
		}

		button[disabled] {
			opacity: 0.5;
		}
	}

	//短信验证码容器
	.sms_code_input_container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.smscode_get_info {
		font-size: 24rpx;
		color: #999999;
		display: flex;
		margin-top: 40rpx;

		view {
			display: flex;

			text {
				color: #FF7541;
				margin-right: 10rpx
			}

			view {
				color: #008CFF;
				margin-left: 16rpx
			}
		}
	}

	//我的权益
	.my_interest {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 47rpx;

		image {
			width: 32rpx;
			height: 32rpx;
		}

		text {
			font-size: 28rpx;
			color: #FF7541
		}
	}
</style>